package com.example.administrator.xiaobo.fragment.LiveDetailsFragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.activity.BoActivity;
import com.example.administrator.xiaobo.adapter.MovieAdapter;
import com.example.administrator.xiaobo.adapter.RecyclerBaseAdapter;
import com.example.administrator.xiaobo.jsonbean.MovieJsonBean;
import com.example.administrator.xiaobo.url.URLAdress;
import com.example.administrator.xiaobo.utils.BaseCallback;
import com.example.administrator.xiaobo.utils.OkHttpHelper;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MovieFragment extends Fragment {

    private OkHttpHelper okHttpHelper = OkHttpHelper.getInstance();
    private List<MovieJsonBean.DataBean> list;
    private RecyclerView recyclerview;
    private MovieAdapter movieAdapter;
    private int pi = 1;
    private int ps = 20;
    private int totalPage;

    private static final int STATE_NORMAL = 0;
    private static final int STATE_REFREH = 1;
    private static final int STATE_MORE = 2;

    private int state = STATE_NORMAL;

    private MaterialRefreshLayout mRefreshLayout;

    private FloatingActionButton fab_Button;
    public MovieFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_movice, container, false);
        recyclerview = (RecyclerView) view.findViewById(R.id.movie_recycler);
        mRefreshLayout = (MaterialRefreshLayout) view.findViewById(R.id.refresh_view);
        fab_Button= (FloatingActionButton) view.findViewById(R.id.fab_Button);
        Indata();
        initRefreshLayout();

        return view;
    }

    private void initRefreshLayout() {
        mRefreshLayout.setLoadMore(true);
        mRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                setRefreshData();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                if (pi <= totalPage)
                    loadMoreData();
                else {
                    Toast.makeText(getActivity(), "没了", Toast.LENGTH_SHORT).show();
                    mRefreshLayout.finishRefreshLoadMore();
                }
            }
        });
    }

    private void loadMoreData() {
        pi = ++pi;
        state = STATE_MORE;

        Indata();
    }

    private void setRefreshData() {
        pi = 1;

        state = STATE_REFREH;
        Indata();
    }

    private void Indata() {


        String url = URLAdress.Movie_URL + "&pi=" + pi + "&ps=" + ps + "&tt=" + System.currentTimeMillis();
        Log.e("CCC",url);
        okHttpHelper.get(url, new BaseCallback() {
            @Override
            public void onBeforeRequest(Request request) {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(String response) {
                Log.e("AAA", response);
                Gson gson = new Gson();
                MovieJsonBean movieJsonBean = gson.fromJson(response, MovieJsonBean.class);
                pi = movieJsonBean.getPinfo().getPi();
                totalPage = movieJsonBean.getPinfo().getPn();
                list = new ArrayList<MovieJsonBean.DataBean>();
                list.addAll(movieJsonBean.getData());
                showData();
            }

            @Override
            public void onError(Response response, int code) {

            }
        });
    }

    private void showData() {

        switch (state) {
            case STATE_NORMAL:
                movieAdapter = new MovieAdapter(getContext(), list);
                recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
                recyclerview.setAdapter(movieAdapter);
                movieAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener() {
                    @Override
                    public void OnClick(View view, int position) {
                        Toast.makeText(getContext(),position+"",Toast.LENGTH_LONG).show();
                        Intent intent=new Intent(getContext(), BoActivity.class);
                        intent.putExtra("id",list.get(position).getId());
                        intent.putExtra("recordparams",list.get(position).getRecordparams());
                        startActivity(intent);
                    }
                });
                fab_Button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        recyclerview.scrollToPosition(0);
                    }
                });
                break;
            case STATE_REFREH:
                movieAdapter.clearData();
                movieAdapter.addData(list);

                recyclerview.scrollToPosition(0);
                mRefreshLayout.finishRefresh();
                break;
            case STATE_MORE:
                movieAdapter.addData(movieAdapter.getDatas().size(), list);
                recyclerview.scrollToPosition(movieAdapter.getDatas().size());
                mRefreshLayout.finishRefreshLoadMore();
                break;
        }

    }
}
